package com.cmy.servlet.user;

import com.cmy.pojo.User;
import com.cmy.service.user.UserServiceImp1;
import com.cmy.util.Constant;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    //Servlet:控制层,调用业务层代码

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入LoginServlet...");
        //获取用户名和密码
        String userName = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");

        //和数据库中的用户密码进行对比,调用业务层
        UserServiceImp1 userService = new UserServiceImp1();
        User loginUser = userService.login(userName, userPassword);//这里已经把登录的人查出来了

        if (loginUser!=null){//查有此人,可以登录
            //将用户的信息放入session中
            req.getSession().setAttribute(Constant.USER_SESSION,loginUser);
            //跳转到内部主页
            resp.sendRedirect("jsp/frame.jsp");
        }else {//查无此人,无法登录
            //转发回登录页面
            req.setAttribute("error","用户名或者密码不正确!");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

    